﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StuManagementSystem
{
    public partial class frm_StuInfoList : Form
    {
        DbContext db = new DbContext();
        private string sql1 = @"SELECT StuInfo.StuCode,StuName ,StuSexName,StuGradeName,StuTelephone FROM dbo.StuInfo,dbo.StuSex,dbo.StuGrade WHERE dbo.StuInfo.StuSexId = dbo.StuSex.StuSexId AND dbo.StuInfo.StuGradeId = dbo.StuGrade.StuGradeId ";
        public frm_StuInfoList()
        {
            InitializeComponent();
        }


        #region 窗口预加载
        /// <summary>
        /// 窗口预加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frm_StuInfoList_Load(object sender, EventArgs e)
        {
            //DataGridView整行选中
            this.dgv_StuInfoList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //DataGridView不允许出现空行
            this.dgv_StuInfoList.AllowUserToAddRows = false;
            //不允许多选
            this.dgv_StuInfoList.MultiSelect = false;
            SelectStuInfo(sql1);
        }

        #endregion

        #region 搜索方法
        /// <summary>
        /// 显示方法
        /// </summary>
        /// <param name="sql"></param>
        private void SelectStuInfo(string sql)
        {
            var ds = db.ExecDataSet(sql.ToString());
            this.dgv_StuInfoList.DataSource = ds.Tables[0];
        }
        #endregion

        #region 右键菜单

        /// <summary>
        /// 修改学生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cms_ChangeStuInfo_Click(object sender, EventArgs e)
        {
            //读取选择的学号
            int StuCode = Convert.ToInt32(this.dgv_StuInfoList.CurrentRow.Cells[0].Value);
            frm_ChangeStuInfo frm = new frm_ChangeStuInfo(StuCode);
            frm.ShowDialog();
        }
        /// <summary>
        /// 删除学生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cms_Delete_Click(object sender, EventArgs e)
        {
            //读取选择的学号
            int StuCode = Convert.ToInt32(this.dgv_StuInfoList.CurrentRow.Cells[0].Value);
            DialogResult isOK = MessageBox.Show("确认删除吗？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (isOK == DialogResult.OK)
            {
                var sql = string.Format(@"
                 IF EXISTS (SELECT COUNT(1) FROM dbo.StuMark WHERE StuCode = {0})
                    DELETE FROM dbo.StuMark WHERE StuCode = {0}
                 DELETE FROM StuInfo WHERE StuCode = {0}", StuCode);
                if (db.ExecNonQuery(sql))
                    MessageBox.Show("删除成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    MessageBox.Show("删除失败！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                SelectStuInfo(sql1);
            }
        }

        /// <summary>
        /// 成绩管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cms_StuMarkManagement_Click(object sender, EventArgs e)
        {
            //读取选择的学号
            int StuCode = Convert.ToInt32(this.dgv_StuInfoList.CurrentRow.Cells[0].Value);
            frm_AddStuMark frm = new frm_AddStuMark(StuCode);
            frm.ShowDialog();
        }
        #endregion

        #region 双击控件时
        /// <summary>
        /// 双击控件时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var No = this.treeView1.SelectedNode;
            var sql = "";
            if (No.Text == "全部")
                return;
            if (No.Text == "男" || No.Text == "女")
                sql = string.Format(sql1 + @" and dbo.StuGrade.StuGradeId = {0} and StuSex.StuSexId = {1} ", No.Parent.Name, No.Name);
            else
                sql = string.Format(sql1 + @" and dbo.StuGrade.StuGradeId = {0}", No.Name);
            SelectStuInfo(sql);
        }
        #endregion

    }
}
